package lingnan.cn.edu.view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;




import lingnan.cn.edu.User.User;
import lingnan.cn.edu.service.MyserviceImpl;

public class LoginView extends BaseView implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1975029832810325814L;

	public LoginView(){
		super(370,560,"登录界面");
	}
	
private JPanel containerPanel,namePanel,passwordPanel;
    
    private JLabel nameLabel,pwdLabel;
    private JTextField nameTF;
    private JPasswordField pwdTF;
    
    private JButton loginBtn;
    
	@Override
	protected void initView() {
		Image bgImage = null;
		try {
			bgImage = ImageIO.read(new File("static\\img\\background.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
        containerPanel = new BGPanel(bgImage);
        containerPanel.setLayout(null);
		
		namePanel = new JPanel();
		
		nameLabel = new JLabel(new ImageIcon("static\\icon\\1.png"));
		nameTF = new JTextField("柯晓璇",15);
		
		//设置文本框样式
		namePanel.setOpaque(false);//设为透明
		//nameTF.setBorder(null);//去掉文本框的边框线
		nameTF.setBackground(Color.white);
		
		nameTF.setPreferredSize(new Dimension(150, 28));
		MatteBorder border = new MatteBorder(2, 2, 2, 2,new Color(175,211,250));
		
		
		nameTF.setBorder(border);
		namePanel.add(nameLabel);
		namePanel.add(nameTF);
		namePanel.setBounds(75,150, 200, 500);
		
		
		passwordPanel = new JPanel();
		pwdLabel = new JLabel(new ImageIcon("static\\icon\\1.png"));
		pwdTF = new JPasswordField("123456",15);
		//设置文本框样式
		passwordPanel.setOpaque(false);//设为透明
		//pwdTF.setBorder(null);//去掉文本框的边框线
		pwdTF.setBackground(Color.white);
		pwdTF.setPreferredSize(new Dimension(150, 28));
		MatteBorder Border = new MatteBorder(2, 2, 4, 2,new Color(175,211,250));
		pwdTF.setBorder(Border);
	
		passwordPanel.add(pwdLabel);
		passwordPanel.add(pwdTF);
		passwordPanel.setBounds(75,200, 200, 30);
		
		loginBtn = new JButton("登录");
		loginBtn.setBounds(150,250, 60, 30);
		loginBtn.addActionListener(this);
		
		containerPanel.add(namePanel);
		containerPanel.add(passwordPanel);
		containerPanel.add(loginBtn);
		
		Container container = getContentPane();
		container.add(containerPanel);
	}
	
	public static void main(String[] args) {
		LoginView loginView = new LoginView();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(loginBtn==e.getSource()) {
			String loginName = nameTF.getText();
			String password = new String(pwdTF.getPassword());
			
			MyserviceImpl myservice = new MyserviceImpl();
			User user = myservice.login(loginName, password);
			if(user==null) {
				JOptionPane.showMessageDialog(this,"账号或密码错误");
			}else { 
				//去到主界面
				new MainView(loginName);
				this.dispose();
			}
			
			
		}
	}
	
	  
}
